<?php

namespace app\api\controller\article;

use app\common\controller\Api;
use think\Request;
use think\Cache;
use app\admin\model\Article;
use app\admin\model\Train;
use app\admin\model\TrainUser;
use app\common\library\Auth;
use GatewayWorker\Lib\Gateway;
use fast\Http;
use fast\Rsa;
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    public function get()
    {
        $articles = Article::order('createtime','desc')->with('user')->paginate(6);
        $this->success('获取成功',$articles);
    }
    public function detail()
    {
        $id = $this->request->get('article_id');
        $article = Article::alias('article')->where('article.id',$id)->with('user')->find();
        $this->success('获取成功',$article);
    }
    public function uploadBase64Image()
    {
        $base64_image = $this->request->post('base64_image'); // 获取Base64字符串
        $train_id = $this->request->post('train_id');
        if ($base64_image) {
            // 解码Base64字符串
            $image_data = base64_decode($base64_image);
            $current_year = date("Y"); // 获取当前年份
            $current_month = date("m"); // 获取当前月份
            $current_day = date("d"); // 获取当前日期
            // 将年、月、日拼接成字符串
            $date_string = $current_year . $current_month . $current_day;
            // 保存图像到服务器
            $save_path = 'uploads/' . $date_string . '/';
            // 创建目录（如果不存在）
            if (!file_exists($save_path)) {
                mkdir($save_path, 0777, true); // 创建多级目录，确保目录存在并有适当的权限
            }
            $file_name = time() . '.png'; // 使用唯一文件名以避免冲突
            // 完整文件路径
            $file_path = $save_path . $file_name;
            if(preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image,$result)){
                $type = $result[2];
                if(!file_exists($save_path)){
                    //检查是否有该文件夹，如果没有就创建，并给予最高权限
                    mkdir($save_path, 0700);
                }
                if (file_put_contents($file_path, base64_decode(str_replace($result[1], '', $base64_image)))){
                    $train = Train::where('id',$train_id)->find();
                    if($train){
                        $trainuser = TrainUser::where('user_id',$this->auth->id)->where('train_id',$train_id)->find();
                        if(!$trainuser){
                            TrainUser::create([
                                'user_id'=>$this->auth->id,
                                'train_id'=>$train->id,
                                'sign'=>'/'.$file_path
                            ]);
                        }


                    }else{
                        return $this->error('签署失败');
                    }
                    return $this->success('签署成功','/'.$file_path);

                }else{
                    return $this->error('签署失败');
                }
            }else{
                return $this->error('签署失败');
            }
        }
    }
    public function getTrain()
    {
        return $this->success('签署成功',Train::all());
    }
    public function signTrain()
    {
        $train_id = $this->request->post('train_id');
        $user_id = $this->auth->id;
        $data = [];
        $train = Train::where('id',$train_id)->find();
        if($train){
            $trainuser = TrainUser::where('user_id',$user_id)->where('train_id',$train_id)->find();
            if($trainuser){

                $data['sign'] = $trainuser->sign;
            }
            $data['word'] = $train->word;
            return $this->success('获取成功',$data);
        }else{
            return $this->error('获取失败');
        }

    }
}